package com.weaver.drools.common.entity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.*;

import org.hibernate.annotations.GenericGenerator;
/**
 * 
 *  @application name:
 *  @author: zhouxinlei 
 *  @time：2018年6月27日
 *  @version：ver 1.1
 */
@Entity
@Table(name = "SYS_ROLE")
public class SysRole implements java.io.Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(generator = "snowFlakeId")
	@GenericGenerator(name = "snowFlakeId", strategy = "com.weaver.drools.common.businesskey.SnowFlakeId")
	@Column(name = "ROLE_ID",nullable = false,length = 20)
	@OrderBy("ROLE_ID asc")
	private Long id;
	@Column(name = "ROLE_CODE",nullable = false,length = 20)
	private String roleCode;
	@Column(name = "ROLE_NAME",nullable = false,length = 20)
	private String roleName;
	@Column(name = "CD_TIME", nullable = false)
    private Date cdTime;
	@Column(name = "UP_TIME", nullable = false)
	private Date upTime;
	@Column(name = "available",nullable = false, length = 1)
    private Long available; // 是否可用,如果不可用将不会添加给用户
	
	@ManyToOne(cascade={CascadeType.REFRESH},fetch=FetchType.EAGER,optional=false)
    @JoinColumn(name = "CD_PERSON", referencedColumnName = "USER_ID", nullable = false)
    private SysUser cdPerson;
    
	@ManyToOne(cascade={CascadeType.REFRESH},fetch=FetchType.EAGER,optional=false)
    @JoinColumn(name = "UP_PERSON", referencedColumnName = "USER_ID", nullable = false)
    private SysUser upPerson;
	
	//角色 -- 用户关系：多对多关系;
	@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
	@JoinTable(name = "USER_ROLE",joinColumns = { @JoinColumn(name = "ROLE_ID", referencedColumnName = "ROLE_ID") }, 
    			inverseJoinColumns = { @JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID") })
	@OrderBy("USER_ID asc")
    private Set<SysUser> users = new TreeSet<SysUser>();
	
    //角色 -- 权限关系：多对多关系;
	@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
	@JoinTable(name = "ROLE_PERMISSION",joinColumns = { @JoinColumn(name = "ROLE_ID", referencedColumnName = "ROLE_ID") }, 
	  			inverseJoinColumns = { @JoinColumn(name = "PERMISSION_ID", referencedColumnName = "PERMISSION_ID") })
	@OrderBy("PERMISSION_ID asc")
    private Set<SysPermission> permissions = new TreeSet<SysPermission>();
	
	
	public SysRole() {
		super();
	}

	public Long getId() {
		return id;
	}

	public String getRoleCode() {
		return roleCode;
	}

	public String getRoleName() {
		return roleName;
	}

	public Date getCdTime() {
		return cdTime;
	}

	public Date getUpTime() {
		return upTime;
	}

	public Long getAvailable() {
		return available;
	}

	public SysUser getCdPerson() {
		return cdPerson;
	}

	public SysUser getUpPerson() {
		return upPerson;
	}

	public Set<SysUser> getUsers() {
		return users;
	}

	public Set<SysPermission> getPermissions() {
		return permissions;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setRoleCode(String roleCode) {
		this.roleCode = roleCode;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public void setCdTime(Date cdTime) {
		this.cdTime = cdTime;
	}

	public void setUpTime(Date upTime) {
		this.upTime = upTime;
	}

	public void setAvailable(Long available) {
		this.available = available;
	}

	public void setCdPerson(SysUser cdPerson) {
		this.cdPerson = cdPerson;
	}

	public void setUpPerson(SysUser upPerson) {
		this.upPerson = upPerson;
	}

	public void setUsers(Set<SysUser> users) {
		this.users = users;
	}

	public void setPermissions(Set<SysPermission> permissions) {
		this.permissions = permissions;
	}
	public List<SysUser> getUserList() {
		if (users!=null) {
			List<SysUser> userList= new ArrayList<SysUser>(users);
	        if (userList!=null&&userList.size()>0) {
	        	Collections.sort(userList, new Comparator<SysUser>() {
	                @Override
	                public int compare(SysUser o1, SysUser o2) {
	                    return o1.getId()<o2.getId() ? -1 :1;
	                }
	            });
			}
	        return userList;
		}
		return null;
	}


	public List<SysPermission> getPermissionList() {
		if (permissions!=null) {
			List<SysPermission> permissionList= new ArrayList<SysPermission>(permissions);
			if (permissionList!=null&&permissionList.size()>0) {
				Collections.sort(permissionList, new Comparator<SysPermission>() {
		            @Override
		            public int compare(SysPermission o1, SysPermission o2) {
		                return o1.getId()<o2.getId() ? -1 :1;
		            }
		        });
			}
			return permissionList;
		}
		return null;
	}

}